#==============================================================================#
# Script by: Tigurus Fay                                                       #
#==============================================================================#
#==============================================================================#                 
# Script: Hearts of Iron 3 Quick Menu                                          #
# Version: v1.1                                                                #
# Install: Press F11 and place the Script directly above [Main].               #
# Description: This menu lets you make a decision to quickly start the game    #
#              With 4 available presets or to go to the default menu.          # 
#==============================================================================#
# Instruction: Use the config below to call your scenario's name.              #
# Use a intro map with an auto-event. Use conditional branches for:            #
# - tutorial                                                                   #
# - scenario1                                                                  #
# - scenario2                                                                  #
# - scenario3                                                                  #
# - scenario4                                                                  #
# As condition: Call Script: "$tutorial != nil and $tutorial == true" for the  #
# Conditions you want to send the player in when pressing the Tutorial Button  #
# For the others, change to $scenario1, 2, 3 or 4.                             #
#==============================================================================#
module Config
  #--------------------------------------------------------------------------
  # * Config - Set names and descriptions for all 4 scenario's.
  #--------------------------------------------------------------------------

    # Set false to enable the tutorial in the Quick Menu
    Disable_Tutorial = false
    
    # Insert 0 to 4 to enable the amount of Presets available.
    # 0: Disables the menu completely
    # 1: Enables only Preset 1
    # 2: Enables Preset 1 and 2
    # 3: Enables Preset 1, 2 and 3
    # 4: Enabled all 4 presets.
    # To change the Config setting in-game. Open an event, call script and type:
    # "$Config_Setting = ..." where the "..." can be the number you want.
    $Config_Setting = 4
  
  #Scenario 1
    PRESETNAME1 = ["Thunder Run"]
      PRESET11 = ["It's time to end this war once and for all!"]
      PRESET12 = ["Use our tanks to meet the allies at Corditz"]
      PRESET13 = ["Capture the general and end this war!"]
      
  #Scenario 2
    PRESETNAME2 = ["Annexation of Sireihil"]
      PRESET21 = ["This vassal has defected from the Empire!"]
      PRESET22 = ["They already fled back to their capital"]
      PRESET23 = ["Let's us show the Emperor's might!"]
      
  #Scenario 3
    PRESETNAME3 = ["Soviet Invasion"]
      PRESET31 = ["The Soviets are invading!"]
      PRESET32 = ["Gain control of our defensive lines"]
      PRESET33 = ["And repel their army!"]
      
  #Scenario 4
    PRESETNAME4 = ["Guard:"]
      PRESET41 = ["I used to be a cheat but then"]
      PRESET42 = ["I took an arrow to the knee"]
      PRESET43 = ["FUS RO DAH!"]
end
    
#==============================================================================
# ** Window_Compilation
#------------------------------------------------------------------------------
#  This includes 4 windows which you can set text at for the presets.
#==============================================================================

class Window_Quick1 < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 288, 128)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end

  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.font.size = 16
    self.contents.draw_text(8, 32, 288, 32, Config::PRESET11.to_s)
    self.contents.draw_text(8, 48, 288, 32, Config::PRESET12.to_s)
    self.contents.draw_text(8, 64, 288, 32, Config::PRESET13.to_s)
  end
end

class Window_Quick2 < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 288, 128)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  
  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.font.size = 16
    self.contents.draw_text(8, 32, 288, 32, Config::PRESET21.to_s)
    self.contents.draw_text(8, 48, 288, 32, Config::PRESET22.to_s)
    self.contents.draw_text(8, 64, 288, 32, Config::PRESET23.to_s)
  end
end

class Window_Quick3 < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 288, 128)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end

  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.font.size = 16
    self.contents.draw_text(8, 32, 288, 32, Config::PRESET31.to_s)
    self.contents.draw_text(8, 48, 288, 32, Config::PRESET32.to_s)
    self.contents.draw_text(8, 64, 288, 32, Config::PRESET33.to_s)
  end
end

class Window_Quick4 < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 288, 128)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end

  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.font.size = 16
    self.contents.draw_text(8, 32, 288, 32, Config::PRESET41.to_s)
    self.contents.draw_text(8, 48, 288, 32, Config::PRESET42.to_s)
    self.contents.draw_text(8, 64, 288, 32, Config::PRESET43.to_s)
  end
end

#==============================================================================
# ** Window_QuickMenu
#------------------------------------------------------------------------------
#  This window displays the Preset Window.
#==============================================================================

class Window_QuickMenu < Window_Selectable
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 64, 640, 416)
    @column_max = 2
    @TitleQuick = commands
    refresh
    self.index = 0
  end

  def refresh
    self.contents.clear
    for i in 0...@item_max
      draw_item(i, normal_color)
    end
  end
  
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #     color : text color
  #--------------------------------------------------------------------------
  def draw_item(index, color)
    self.contents.font.color = color
    rect = Rect.new(4, 32 * index, self.contents.width - 8, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    self.contents.draw_text(rect, @TitleQuick[index])
  end
  #--------------------------------------------------------------------------
  # * Disable Item
  #     index : item number
  #--------------------------------------------------------------------------
  def disable_item(index)
    draw_item(index, disabled_color)
  end
end

#============================================================================== 
# ** Window_Command_Preset 
#------------------------------------------------------------------------------ 
# This window deals with general command choices. 
#============================================================================== 

class Window_Command_Preset < Window_Selectable 
 #-------------------------------------------------------------------------- 
 # * Object Initialization 
 # width : window width 
 # commands : command text string array 
 #-------------------------------------------------------------------------- 
 def initialize(width, commands) 
 # Compute window height from command quantity 
   super(0, 0, 576, 256)
     @item_max = commands.size 
     @single_width = width 
     @column_max = 2
     @commands = commands 
     self.contents = Bitmap.new(576 - 32 , 256 - 32)
     refresh 
     self.index = 0 
   end 
 #-------------------------------------------------------------------------- 
 # * Refresh 
 #-------------------------------------------------------------------------- 
   def refresh 
     self.contents.clear 
       for i in 0...@item_max 
       draw_item(i, normal_color) 
      end 
   end 
 #-------------------------------------------------------------------------- 
 # * Draw Item 
 # index : item number 
 # color : text color 
 #-------------------------------------------------------------------------- 
 def draw_item(index, color) 
   self.contents.font.color = color 
   x = 4 + index % 2 * 288
   y = index / 2 * 128
   rect = Rect.new(x, y, self.width / @column_max - 32, 32)
   self.contents.fill_rect(rect, Color.new(0, 0, 0, 0)) 
   self.contents.draw_text(rect, @commands[index],1) 
 end 
#==============================================================================
# ** Cursor_update
#=============================================================================
  def update_cursor_rect
    # If cursor position is less than 0
    if @index < 0
      self.cursor_rect.empty
      return
    end
    # Get current row
    row = @index / @column_max
    # If current row is before top row
    if row < self.top_row
      # Scroll so that current row becomes top row
      self.top_row = row
    end
    # If current row is more to back than back row
    if row > self.top_row + (self.page_row_max - 1)
      # Scroll so that current row becomes back row
      self.top_row = row - (self.page_row_max - 1)
    end
    # Calculate cursor width
    cursor_width = self.width / @column_max - 32
    # Calculate cursor coordinates
    x = @index % @column_max * (cursor_width + 32)
    y = @index / @column_max * 128 - self.oy
    # Update cursor rectangle
    self.cursor_rect.set(x, y, cursor_width, 96)
  end
 #-------------------------------------------------------------------------- 
 # * Disable Item 
 # index : item number 
 #-------------------------------------------------------------------------- 
   def disable_item(index) 
     draw_item(index, disabled_color) 
   end 
end
 
#==============================================================================
# ** Scene_QuickMenu
#------------------------------------------------------------------------------
#  This class performs quick title menu processing.
#==============================================================================

class Scene_QuickMenu
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
 
  def main
   $game_variables.id[1]
    # Load database
    $data_actors        = load_data("Data/Actors.rxdata")
    $data_classes       = load_data("Data/Classes.rxdata")
    $data_skills        = load_data("Data/Skills.rxdata")
    $data_items         = load_data("Data/Items.rxdata")
    $data_weapons       = load_data("Data/Weapons.rxdata")
    $data_armors        = load_data("Data/Armors.rxdata")
    $data_enemies       = load_data("Data/Enemies.rxdata")
    $data_troops        = load_data("Data/Troops.rxdata")
    $data_states        = load_data("Data/States.rxdata")
    $data_animations    = load_data("Data/Animations.rxdata")
    $data_tilesets      = load_data("Data/Tilesets.rxdata")
    $data_common_events = load_data("Data/CommonEvents.rxdata")
    $data_system        = load_data("Data/System.rxdata")
    # Make system object
    $game_system = Game_System.new
    # Make title graphic
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.title($data_system.title_name)
    @quick1_window = Window_Quick1.new
    @quick1_window.back_opacity = 160
    @quick1_window.x = 32
    @quick1_window.y = 90
    @quick2_window = Window_Quick2.new
    @quick2_window.back_opacity = 160
    @quick2_window.x = 320
    @quick2_window.y = 90
    @quick3_window = Window_Quick3.new
    @quick3_window.back_opacity = 160
    @quick3_window.x = 32
    @quick3_window.y = 218
    @quick4_window = Window_Quick4.new
    @quick4_window.back_opacity = 160
    @quick4_window.x = 320
    @quick4_window.y = 218
    s1 = Config::PRESETNAME1.to_s
    s2 = Config::PRESETNAME2.to_s
    s3 = Config::PRESETNAME3.to_s
    s4 = Config::PRESETNAME4.to_s
    d1 = "Tutorial"
    d2 = "Custom Menu"
    d3 = "Shutdown"
    @command_window = Window_Command.new(192, [d1, d2, d3])
    @command_window.back_opacity = 160
    @command_window.x = 320 - @command_window.width / 2
    @command_window.y = 350
    @command_window.active = false
    @command_window.index = -1
    case $Config_Setting
    when 0
        @quick_window = Window_Command_Preset.new(192, [])
        @quick1_window.contents.clear
        @quick2_window.contents.clear
        @quick3_window.contents.clear
        @quick4_window.contents.clear
        @command_window.active = true
        @command_window.index = 0
        @quick_window.active = false
        @quick_window.index = -1
    when 1
        @quick_window = Window_Command_Preset.new(192, [s1])
        @quick2_window.contents.clear
        @quick3_window.contents.clear
        @quick4_window.contents.clear
    when 2
        @quick_window = Window_Command_Preset.new(192, [s1, s2])
        @quick3_window.contents.clear
        @quick4_window.contents.clear
    when 3
        @quick_window = Window_Command_Preset.new(192, [s1, s2, s3])
        @quick4_window.contents.clear
    when 4
        @quick_window = Window_Command_Preset.new(192, [s1, s2, s3, s4])
    end
    @quick_window.back_opacity = 0
    @quick_window.x = 32
    @quick_window.y = 90
    if Config::Disable_Tutorial  
      @command_window.disable_item(0)
    end
    # Play title BGM
    $game_system.bgm_play($data_system.title_bgm)
    # Stop playing ME and BGS
    Audio.me_stop
    Audio.bgs_stop
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of command window
    @quick_window.dispose
    @command_window.dispose
    @quick1_window.dispose
    @quick2_window.dispose
    @quick3_window.dispose
    @quick4_window.dispose
    # Dispose of title graphic
    @sprite.bitmap.dispose
    @sprite.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update command windows
    @quick_window.update
    @command_window.update
    @quick1_window.update
    @quick2_window.update
    @quick3_window.update
    @quick4_window.update
    # If command window is active: call update_command
    if @quick_window.active
      update_quick
      return
    end
    # If status window is active: call update_status
    if @command_window.active
      update_command
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when quick window is active)
  #--------------------------------------------------------------------------
  def update_quick
    # If B button was pressed
    if Input.trigger?(Input::B)
      @command_window.active = true
      @command_window.index = 0
      @quick_window.active = false
      @quick_window.index = -1
    end  
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @quick_window.index
      when 0  # Scenario 1
        command_scenario
      when 1  # Scenario 2
        command_scenario
      when 2  # Scenario 3
        command_scenario
      when 3  # Scenario 4
        command_scenario
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when command_window is active)
  #--------------------------------------------------------------------------
  
  def update_command
    if Input.trigger?(Input::B)
      # Play cancel SE
       $game_system.se_play($data_system.cancel_se)
       @command_window.active = false
       @command_window.index = -1
       @quick_window.active = true
       @quick_window.index = 0
      return
    end
    
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window.index
      when 0  # Tutorial
        command_scenario
      when 1  # Return to default title
        command_return
      when 2  # Shutdown
        command_shutdown
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Command: Scenario 
  #--------------------------------------------------------------------------
  def command_scenario
    $game_system.se_play($data_system.decision_se)
    # Stop BGM
    Audio.bgm_stop
    # Reset frame count for measuring play time
    Graphics.frame_count = 0
    # Make each type of game object
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_screen        = Game_Screen.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    # Set up initial party
    $game_party.setup_starting_members
    # Set up initial map position
    $game_map.setup($data_system.start_map_id)
    # Move player to initial position
    $game_player.moveto($data_system.start_x, $data_system.start_y)
    # Refresh player
    $game_player.refresh
    # Run automatic change for BGM and BGS set with map
    $game_map.autoplay
    # Update map (run parallel process event)
    $game_map.update
    # Switch to map screen      
    case @quick_window.index
      when 0     
        $scenario1 = true
        $scene = Scene_Map.new
      when 1
        $scenario2 = true
        $scene = Scene_Map.new
      when 2
        $scenario3 = true
        $scene = Scene_Map.new
      when 3
        $scenario4 = true
        $scene = Scene_Map.new
    end
    if Config::Disable_Tutorial  
         $game_system.se_play($data_system.buzzer_se)
      return
    end
      case @command_window.index
        when 0  # Tutorial
          $tutorial = true
          $scene = Scene_Map.new
      end
  end
  #--------------------------------------------------------------------------
  # * Command: return
  #--------------------------------------------------------------------------
    def command_return
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Turn to Custom Menu
      $scene = Scene_Title.new
    end
  #--------------------------------------------------------------------------
  # * Command: Shutdown
  #--------------------------------------------------------------------------
 def command_shutdown
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Fade out BGM, BGS, and ME
    Audio.bgm_fade(800)
    Audio.bgs_fade(800)
    Audio.me_fade(800)
    # Shutdown
    $scene = nil
  end
end

#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs title screen processing.
#==============================================================================

class Scene_Title
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Load database
    $data_actors        = load_data("Data/Actors.rxdata")
    $data_classes       = load_data("Data/Classes.rxdata")
    $data_skills        = load_data("Data/Skills.rxdata")
    $data_items         = load_data("Data/Items.rxdata")
    $data_weapons       = load_data("Data/Weapons.rxdata")
    $data_armors        = load_data("Data/Armors.rxdata")
    $data_enemies       = load_data("Data/Enemies.rxdata")
    $data_troops        = load_data("Data/Troops.rxdata")
    $data_states        = load_data("Data/States.rxdata")
    $data_animations    = load_data("Data/Animations.rxdata")
    $data_tilesets      = load_data("Data/Tilesets.rxdata")
    $data_common_events = load_data("Data/CommonEvents.rxdata")
    $data_system        = load_data("Data/System.rxdata")
    # Make system object
    $game_system = Game_System.new
    # Make title graphic
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.title($data_system.title_name)
    # Make command window
    s1 = "Quick Menu"
    s2 = "Continue"
    s3 = "Shutdown"
    @command_window = Window_Command.new(192, [s1, s2, s3])
    @command_window.back_opacity = 160
    @command_window.x = 320 - @command_window.width / 2
    @command_window.y = 288
    # Continue enabled determinant
    # Check if at least one save file exists
    # If enabled, make @continue_enabled true; if disabled, make it false
    @continue_enabled = false
    for i in 0..3
      if FileTest.exist?("Save#{i+1}.rxdata")
        @continue_enabled = true
      end
    end
    # If continue is enabled, move cursor to "Continue"
    # If disabled, display "Continue" text in gray
    if @continue_enabled
      @command_window.index = 1
    else
      @command_window.disable_item(1)
    end
    # Play title BGM
    $game_system.bgm_play($data_system.title_bgm)
    # Stop playing ME and BGS
    Audio.me_stop
    Audio.bgs_stop
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of command window
    @command_window.dispose
    # Dispose of title graphic
    @sprite.bitmap.dispose
    @sprite.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update command window
    @command_window.update
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window.index
      when 0  # Custom Menu
        command_preset
      when 1  # Continue
        command_continue
      when 2  # Shutdown
        command_shutdown
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Command: Preset
  #--------------------------------------------------------------------------
    def command_preset
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Turn to Custom Menu
      $tutorial = false
      $scenario1 = false
      $scenario2 = false
      $scenario3 = false
      $scenario4 = false
      $scene = Scene_QuickMenu.new
    end
  #--------------------------------------------------------------------------
  # * Command: Continue
  #--------------------------------------------------------------------------
  def command_continue
    # If continue is disabled
    unless @continue_enabled
      # Play buzzer SE
      $game_system.se_play($data_system.buzzer_se)
      return
    end
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Switch to load screen
    $scene = Scene_Load.new
  end
  #--------------------------------------------------------------------------
  # * Command: Shutdown
  #--------------------------------------------------------------------------
  def command_shutdown
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Fade out BGM, BGS, and ME
    Audio.bgm_fade(800)
    Audio.bgs_fade(800)
    Audio.me_fade(800)
    # Shutdown
    $scene = nil
  end
end